<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Model\Sale_promotion;
use App\Model\Gprice;
use App\Model\Goods;
use DB;
use App\Http\Controllers\Home\Index\Api\IndexApi;

class StartPromotion implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $salePromotion;
    protected $goodsPrice;
    protected $indexApi;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Sale_promotion $salePromotion, Gprice $goodsPrice, IndexApi $indexApi)
    {
        $this->salePromotion = $salePromotion;
        $this->goodsPrice = $goodsPrice;
        $this->indexApi = $indexApi;
    }

    /**
     * 执行促销开始,修改goods_price表的价格
     *
     * @return void
     */
    public function handle()
    {   
        //获取执行本任务时的时间,并且转化成以分钟为单位,向下取整
        $nowTime = time();
        // $nowTimeMinute = floor($nowTime/60);    //向下取整以分钟为单位

        //获取数据库中促销开始的时间
        $executeTime = $this->salePromotion->sale_start_time;
        // $executeTimeMinute = floor($executeTime/60);    //向下取整以分钟为单位
        
        //调试,把执行时间和促销开始时间存入score表,以便观察
        // $res = DB::table('score')->insert([

        //     'attr1' => date('Y-m-d H:i:s', $nowTime) . ' / '. date('Y-m-d H:i:s', $executeTime),
        //     ]);

        //满足以下条件,则修改goods_price表的discount_price等于sale_promotion的sale_pirce,以及is_discount变为1(正在促销)
        //1.执行本任务的时间与促销开始时间一致(误差正负10秒)
        //2.sale_promotion表的state不等于2(失效或过期)
        if ( ( ($nowTime + 10 >= $executeTime) && ($nowTime - 10 <= $executeTime) ) && ($this->salePromotion->state !== 2) ) {

            //调试,把执行时间和促销结束时间存入score表,以便观察
            // $res = DB::table('score')->insert([

            // 'attr1' => date('Y-m-d H:i:s', $nowTime) . ' / '. date('Y-m-d H:i:s', $executeTime),
            // 'attr2' => '222'
            // ]);

            $this->goodsPrice->discount_price = $this->salePromotion->sale_price;   //修改促销价格
            $this->goodsPrice->is_discount = 1;     //修改促销状态为"正在促销"
            $this->goodsPrice->save();

            $this->salePromotion->state = 1;    //修改促销表的状态为"正在促销";
            $this->salePromotion->save();

            //把有关于Sale_promotion表的Redis都清除
            // Redis::del('homePromotion');
            // Redis::del('indexAsidePromotion');
            // Redis::del('homeHotSale');

            //重置有关于Sale_promotion表的所有Redis
            $this->indexApi->setHotSale();
            $this->indexApi->setAsidePromotion();
            $this->indexApi->setHomePromotion();
            $this->indexApi->delLazyLoadRedis();    //清除懒加载的Redis
            $this->indexApi->delHotDataByGid($this->goodsPrice->gid);   //清除Top30之一的Redis
            // $indexApi = new IndexApi();
            // $indexApi->setHotSale();
            // $indexApi->setAsidePromotion();
            // $indexApi->setHomePromotion();

            //删除此商品的哈希Redis
            // Redis::hDel('hotData', 'hotData:'.$this->goodsPrice->gid );
            
        }

    }
}
